
#include <cstdio>
#include "member_BLineStripe.h"

using namespace BWE;

member_BLineStripe::member_BLineStripe(BLineStripe* polyStripe)
{
	boss = polyStripe;
	thickness = 0;
	closeLoop = false;
	cornerVisible = false;
	length = 0;
	radius = 0;
	dirty = false;
}
member_BLineStripe::~member_BLineStripe()
{

}

void member_BLineStripe::freshCorner(CornerArc& corner, const BVector& vertex, const BVector& prev, const BVector& next)
{
	corner.realRadius = radius;
	if (corner.radius > REAL_EPSILON)
		corner.realRadius = corner.radius;

	if (corner.realRadius <= REAL_EPSILON)
	{
		corner.center = vertex;
		corner.vertices.clear();
		return;
	}


	BVector vecA = (prev - vertex).normal();
	BVector vecB = (next - vertex).normal();
	if (vecA.cross(vecB).length() < REAL_EPSILON)
	{
		corner.center = vertex;
		corner.vertices.clear();
		return;
	}

	corner.axis = vecB.cross(vecA).normal();
	corner.direct = -(vecA + vecB).normal();
	BReal halfAngle = vecA.angle(vecB) * BReal(0.5);
	corner.halfAngle = PI * BReal(0.5) - halfAngle;
	corner.length = corner.realRadius / tan(halfAngle);
	BReal length = corner.realRadius / sin(halfAngle);
	corner.center = vertex - corner.direct * length;

	BVector vertexA = vertex + vecA * corner.length;
	BVector vertexB = vertex + vecB * corner.length;

	corner.vertices.reset();
	corner.vertices.append(vertexA);
	BVector vec = vertexA - corner.center;
	BQuater quater(corner.axis, corner.halfAngle / 5);
	for (int i = 1; i < 10; i++)
	{
		vec *= quater;
		corner.vertices.append(corner.center + vec);
	}
	corner.vertices.append(vertexB);
}
